package com.tledu.student_manager.dao;

import com.tledu.student_manager.exception.CustomException;
import com.tledu.student_manager.model.User;
import com.tledu.student_manager.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cyrus
 */
public class UserDao {

    public User getByUsername(String username) throws CustomException {
        // 和数据库相关的操作
        Connection connection = DBUtil.getConnection();
        String sql = "select * from user where username = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            User user = null;
            while (resultSet.next()) {
                user = new User();

                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                user.setBirthday(resultSet.getDate("birthday"));
            }
            preparedStatement.close();
            connection.close();
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("创建statement出错");
        }
    }

    public List<User> list() throws CustomException {
        // 和数据库相关的操作
        Connection connection = DBUtil.getConnection();
        String sql = "select * from user ";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            List<User> list = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User();

                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                user.setBirthday(resultSet.getDate("birthday"));
                list.add(user);
            }
            preparedStatement.close();
            connection.close();
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("创建statement出错");
        }
    }


    public void delete(Integer id) throws CustomException {
        // 和数据库相关的操作
        Connection connection = DBUtil.getConnection();
        String sql = "delete from user where id=?;";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("创建statement出错");
        }
    }

    public void add(User stu) throws CustomException {
        // 新增的操作. jdbc
        // 1. 注册驱动
        Connection conn = DBUtil.getConnection();
        // 3. 获取Statement
        String sql = "insert into user (username, password, nickname, age, birthday) VALUE (?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            // 设置属性
            preparedStatement.setString(1, stu.getUsername());
            preparedStatement.setString(2, stu.getPassword());
            preparedStatement.setString(3, stu.getNickname());
            preparedStatement.setInt(4, stu.getAge());
            preparedStatement.setDate(5, new java.sql.Date(stu.getBirthday().getTime()));
            // 执行sql语句
            preparedStatement.execute();
            preparedStatement.close();
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据操作错误");
        }

    }
}
